﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.DBUtility;

public partial class Hospital_TraBackPlanList : System.Web.UI.Page, HZ.MBSM.BLL.TraDes
{
    public string strConditions;
    public int projectid;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (HZ.MBSM.BLL.Login.CheckAssert(this.UseAccountType))
        {
            HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
            projectid = SysFun.ToInt(SysFun.IsSQL(Request.QueryString["projectid"]));
            if (!IsPostBack)
            {

                DrBuyStatus.Items.Add("全部");
                DrBuyStatus.Items.Add("未选退货药品");
                DrBuyStatus.Items.Add("已选退货药品未提交");
                DrBuyStatus.Items.Add("已提交给配送人");
            }

            //<!--删除退货单,已经选定退货药品的和提交的退货单不能删除
            if (Request.Form["butDel"] == "删除" && Request.Form["chk"] != null)
            {
                string BuyPlanIdDel = "";
                BuyPlanIdDel = SysFun.ToTrim(Request.Form["chk"]);
                Db db = new Db();
                if (BuyPlanIdDel != "")
                {
                    string[] BGID = BuyPlanIdDel.Length == 0 ? new string[] { "" } : BuyPlanIdDel.Substring(0).Split(new char[] { ',' });

                    string strSql = "";
                    for (int i = 0; i < BGID.Length; i++)
                    {
                        strSql += "@" + "delete  from BackPlan  where BackPlanId=" + BGID[i].ToString() + " and BuyStatus='0'";
                    }
                    if (strSql.Length > 0)
                    {
                        HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "删除", "BackPlan", strSql, "../Hospital/TraBackPlanList.aspx");
                        db.ExecuteTransaction(strSql.Substring(1).Split(new char[] { '@' }), 30);
                    }
                }
                db.Close();
                db = null;
            }
            //删除退货单,已经选定退货药品的和提交的退货单不能删除-->    

            //<!--提交采购单 ,已经提交的退货单不能继续选择退货药品，未采购药品的采购单不能提交
            //if (Request.Form["butTJ"] == "提 交" && Request.Form["chk"] != null)
            //{
            //    string BuyPlanIdTJ = "";
            //    BuyPlanIdTJ = SysFun.ToTrim(Request.Form["chk"]);
            //    Db db = new Db();
            //    if (BuyPlanIdTJ != "")
            //    {
            //        string[] BGID = BuyPlanIdTJ.Length == 0 ? new string[] { "" } : BuyPlanIdTJ.Substring(0).Split(new char[] { ',' });
            //        string strSql = "";
            //        for (int i = 0; i < BGID.Length; i++)
            //        {
            //            strSql += "@" + "update  BackPlan set BuyStatus='2'  where BackPlanId=" + BGID[i].ToString() + " and BuyStatus='1'";
            //        }
            //        if (strSql.Length > 0)
            //        {
            //            db.ExecuteTransaction(strSql.Substring(1).Split(new char[] { '@' }), 30);
            //        }
            //    }
            //    db.Close();            
            //}
            //提交采购单 ,已经提交的退货单不能继续选择退货药品，未采购药品的采购单不能提交-->

            showGridView();
        }
    }
    public void showGridView()
    {

        strConditions = fctConditions();
        Db db = new Db();
        DbPage dbPage = new DbPage(db);
        dbPage.Table = "BackPlan";                                               //要查询的表明
        dbPage.Key = "BackPlanId";                                          //表的主键    
        dbPage.Where = strConditions;                                           //查询条件    
        dbPage.SelectField = "BackPlanId,PlanName,StartTime,BuildTime,case BuyStatus when  '0' then '未选退货药品' when '1'then '已选退货药品未提交' when '2' then '已提交给配送人'   end BuyStatus";//要显示的字段
        dbPage.PageIndex = SysFun.ToInt(Request["CurrentlyPageIndex"]);          //要显示第几页   
        dbPage.PageSize = 5;                                                    //没有显示记录条数   
        dbPage.OrderBy = "BuildTime ,BackPlanId ";                  //排序字段
        dbPage.OrderByIsDesc = true;
        System.Data.IDataReader dr = dbPage.ExecuteReader();
        DlAttorney.DataSource = dr;
        DlAttorney.DataBind();
        dr.Close();
        dr.Dispose();
        dbPage.Db.Close();
        db.Close();
        this.DbPageFlip1.DbPage = dbPage;//将数据传给控键
    }
    /// <summary>
    /// 生成查询条件
    /// </summary>
    /// <returns></returns>
    public string fctConditions()
    {
        HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
        string _strTemp = " projectid=" + projectid + " and AccountId_YY=" + _Account.AccountId + " and";

        //退货单状态
        string BuyStatus = DrBuyStatus.SelectedValue.ToString();                
        switch (BuyStatus)
        {
            case "全部":
                break;
            case "未选退货药品":
                _strTemp += " BuyStatus=0 and";
                break;
            case "已选退货药品未提交":
                _strTemp += " BuyStatus=1 and";
                break;
            case "已提交给配送人":
                _strTemp += " BuyStatus=2 and";
                break;
        }

        //退货单名称
        string _strPlanName = txtPlanName.Text.Trim();
        if (_strPlanName != "")
        {
            _strTemp += " PlanName like '%" + _strPlanName + "%' and";
        }

        //起始时间
        string  _strStartTime = txtStartTime.Text.ToString();
        if (_strStartTime != "")
        {
            _strTemp += " StartTime>'" + _strStartTime + "' and";
        }

        //截至时间
        string _strEndTime = txtEndTime.Text.ToString();
        if (_strEndTime != "")
        {
            _strTemp += " StartTime<'" + _strEndTime + "' and";
        }
        
        if (_strTemp != "")
        {
            _strTemp = "  " + _strTemp.Substring(0, _strTemp.Length - 3);
        }
        return _strTemp;
    }


 


    //protected void BtnDel_Click(object sender, EventArgs e)
    //{
    //    string BuyPlanIdDel = "";
    //    BuyPlanIdDel = SysFun.ToTrim(Request.Form["chk"]);
    //    Db db = new Db();
    //    System.Data.IDataReader dr;
    //    //是否已提交


    //    if (BuyPlanIdDel != "")
    //    {
    //        string[] BGID = BuyPlanIdDel.Length == 0 ? new string[] { "" } : BuyPlanIdDel.Substring(0).Split(new char[] { ',' });

    //        string strSql = "";
    //        for (int i = 0; i < BGID.Length; i++)
    //        {

    //            strSql += "@" + "delete  from BackPlan  where BackPlanId=" + BGID[i].ToString() + " and BuyStatus='0'";
    //        }
    //        if (strSql.Length > 0)
    //        {
    //            db.ExecuteTransaction(strSql.Substring(1).Split(new char[] { '@' }), 30);
    //            //Response.Write(strSql);
    //            // Response.End();
    //        }
    //    }
    //    Response.Write("<script>window.location.href='TraBackPlanList.aspx?projectid=" + projectid + "';</script>");
    //    db.Close();

    //}
    //protected void Button2_Click(object sender, EventArgs e)
    //{
    //    string BuyPlanIdDel = "";
    //    BuyPlanIdDel = SysFun.ToTrim(Request.Form["chk"]);
    //    Db db = new Db();
    //    System.Data.IDataReader dr;
    //    //是否已提交


    //    if (BuyPlanIdDel != "")
    //    {
    //        string[] BGID = BuyPlanIdDel.Length == 0 ? new string[] { "" } : BuyPlanIdDel.Substring(0).Split(new char[] { ',' });

    //        string strSql = "";
    //        for (int i = 0; i < BGID.Length; i++)
    //        {

    //            strSql += "@" + "update  PurchasePlan set BuyStatus='2'  where BuyPlanId=" + BGID[i].ToString() + " and BuyStatus='1'";
    //        }
    //        if (strSql.Length > 0)
    //        {
    //            db.ExecuteTransaction(strSql.Substring(1).Split(new char[] { '@' }), 30);
    //            //Response.Write(strSql);
    //            // Response.End();
    //        }
    //    }
    //    Response.Write("<script>window.location.href='TraPurchasePlanList.aspx?projectid=" + projectid + "';</script>");
    //    db.Close();
    //}
    #region TraDes 成员

    public string Name
    {
        get { return "退货单列表"; }
    }

    public string Describe
    {
        get { return "退货单列表"; }
    }

    public string ParentName
    {
        get { return "退货管理"; }
    }

    public System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> UseAccountType
    {
        get
        {
            System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
            _AccountType.Add(HZ.MBSM.DAL.AccountType.enumHospitale);
            return _AccountType;
        }
    }

    #endregion
}